<?php
	class GP_Graph
	{
		// Constants
		const BASE = 'https://chart.apis.google.com/chart?';

		// Variables
		public $types = array(
								'pie' => 'p',
								'pie3D' => 'p3',
								'line' => 'lc',
								'sparkline' => 'ls',
								'bar-horizontal' => 'bhg',
								'bar-vertical' => 'bvg',
							);

		public $type;
		public $title;
		public $data = array();
		public $size = array();
		public $color = array();
		public $fill = array();
		public $labelsXY = false;
		public $legend;
		public $useLegend = true;
		public $background = 'a,s,ffffff';
		public $max;
		public $min;

		public $query = array();

		// debug
		public $debug = array();

		// Return string
		public function __toString()
		{
			return $this -> afficher();
		}


		/** Create chart
		*/
		public function afficher()
		{
			// Create query
			$this -> query = array(
								'cht'	 => $this -> types[strtolower($this -> type)],					// Type
								'chtt'	 => $this -> title,											// Title
								'chd' => 't:'.$this -> data['values'],								// Data
								'chl'   => $this -> data['names'],									// Data labels
								'chdl' => ( ($this -> useLegend) && (is_array($this -> legend)) ) ? implode('|',$this -> legend) : null, // Data legend
								'chds' => $this -> min.','.$this -> max,								// Data scale
								'chs'   => $this -> size[0].'x'.$this -> size[1],						// Size
								'chco'   => preg_replace( '/[#]+/', '', implode(',', $this -> color)), // Color ( Remove # from string )
								'chm'   => preg_replace( '/[#]+/', '', implode('|', $this -> fill)),   // Fill ( Remove # from string )
								'chxt' => ( $this -> labelsXY == true) ? 'y' : null,				// X & Y axis labels
								//'chxr' => '0,10,30',												// Data range
								'chxr' => '0,' . ($this -> max/2) . ',' . $this -> max,						// Data range
								'chf' => preg_replace( '/[#]+/', '', $this -> background),			// Background color ( Remove # from string )
							);

			// Return chart
			return $this -> img(
						GP_Graph::BASE . http_build_query($this -> query),
						$this -> title
					);
		}

		/** Set attributes
		*/
		public function setChartAttrs( $attrs )
		{
			// debug
			$this -> debug[] = $attrs;

			foreach( $attrs as $key => $value )
				$this -> {"set$key"}($value);
		}

		/** Set type
		*/
		public function setType( $type )
		{
			$this -> type = $type;
		}


		/** Set title
		*/
		public function setTitle( $title )
		{
			$this -> title = $title;
		}


		/** Set data
		*/
		public function setData( $data )
		{
			// Clear any previous data
			unset( $this -> data );

			// Check if multiple data
			if( is_array(reset($data)) )
			{
				/** Multiple sets of data
				*/
				foreach( $data as $key => $value )
				{
					// Set highest & lowest
					foreach( $value as $valueS ) 
					{
						if( empty($this -> min) || $valueS < $this -> min )
							$this -> min = $valueS;
						if( empty($this -> max) || $valueS > $this -> max )
							$this -> max = $valueS;
					}
					// Add data values
					$this -> data['values'][] = implode( ',', $value );

					// Add data names
					$this -> data['names'] = implode( '|', array_keys( $value ) );
				}
				/** Implode data correctly
				*/
				$this -> data['values'] = implode('|', $this -> data['values']);
				/** Create legend
				*/
				$this -> legend = array_keys( $data );
			}
			else
			{
				// Set highest & lowest
				foreach( $data as $value ) 
				{
					if( empty($this -> min) || $value < $this -> min )
						$this -> min = $value;
					if( empty($this -> max) || $value > $this -> max )
						$this -> max = $value;
				}
				/** Single set of data
				*/
				// Add data values
				$this -> data['values'] = implode( ',', $data );

				// Add data names
				$this -> data['names'] = implode( '|', array_keys( $data ) );
			}

		}

		/** Set legend
		*/
		public function setLegend( $legend )
		{
			$this -> useLegend = $legend;
		}

		/** Set size
		*/
		public function setSize( $width, $height = null )
		{
			// check if width contains multiple params
			if(is_array( $width ) )
				$this -> size = $width;
			else
			{
				// set each individually
				$this -> size[] = $width;
				$this -> size[] = $height;
			}
		}

		/** Set color
		*/
		public function setColor( $color )
		{
			$this -> color = $color;
		}

		/** Set labels
		*/
		public function setLabelsXY( $labels )
		{
			$this -> labelsXY = $labels;
		}

		/** Set fill
		*/
		public function setFill( $fill )
		{
			// Fill must have atleast 4 parameters
			if( count( $fill ) < 4 )
			{
				// Add remaining params
				$count = count( $fill );
				for( $i = 0 ; $i < $count ; ++$i )
					$fill[$i] = 'b,' . $fill[$i] . ',' . $i . ',' . ($i + 1) . ',0';
			}
			$this -> fill = $fill;
		}


		/** Set background
		*/
		public function setBackground( $background )
		{
			$this -> background = 'bg,s,' . $background;
		}

		/** Create img html tag
		*/
		public function img( $url, $alt = null )
		{
			return sprintf('<img src="%s" alt="%s" style="width:%spx;height:%spx;" />', $url, $alt, $this -> size[0], $this -> size[1]);
		}


	}
?>